<?php
if(!interface_exists('XPartitionStrategy', false)){
	require(dirname(__FILE__).DIRECTORY_SEPARATOR.'XPartitionStrategy.php');
}
class XModPartitionStrategy implements XPartitionStrategy{
	public function partition(XAnnotation $annotations, $value){
		$name = $annotations->get('Partition.name');
		$dividend = $annotations->get('Strategy.dividend');
		$length = $annotations->get('Strategy.length');
		if(is_null($dividend) || is_null($length)){
			throw new XORMException("Partition Mod Strategy requires correct dividend or length in annotations.");
		}
		$result = intval($value) % intval($dividend);
		$result = $name.'_'.str_repeat('0', intval($length)-strlen($result)).$result;
		return $result;
	}
	
	public function allPartitions(XAnnotation $annotations){
		$name = $annotations->get('Partition.name');
		$dividend = $annotations->get('Strategy.dividend');
		$length = $annotations->get('Strategy.length');
		if(is_null($dividend) || is_null($length)){
			throw new XORMException("Partition Mod Strategy requires correct dividend or length in annotations.");
		}
		$tables = array();
		$range = range(0, intval($dividend) - 1);
		foreach($range as $value){
			$tables[] = $name.'_'.str_repeat('0', $length-strlen($value)).$value;
		}
		return $tables;
	}
	
	public function totalCount(XAnnotation $annotations){
		$dividend = $annotations->get('Strategy.dividend');
		if(is_null($dividend)){
			throw new XORMException("Partition Mod Strategy requires correct dividend or length in annotations.");
		}
		return intval($dividend);
	}
}
?>